package twoD.IsoPfem;

import inf.v3d.view.Viewer;
import twoD.IsoPfem.math.FunctionRToR;
import twoD.IsoPfem.math.Interval;

public class SimpleBeamDemo {
	public static void main(String[] args) {
		ElasticStructure struct = new SimpleBeamDemo().createModel2();

		struct.solveLinearStaticEquilibrium();
		struct.listingDisplacements();

		Viewer view = new Viewer();
		Visualizer viz = new Visualizer(struct, view);
		double d_max = viz.automaticScale();
		viz.drawConstraints();
		viz.drawForces();
		viz.drawEdgeLoads();
		viz.drawElements();

		viz.postAutomaticScale(d_max);
		viz.drawDisplacedStructure();
		viz.drawSigma11();

		view.setVisible(true);
	}

	public ElasticStructure createModel() {

		Mesh M = new Mesh();

		// nodes
		Node n0 = M.addNode(new Node(0, 0));
		Node n1 = M.addNode(new Node(1, 0));
		Node n2 = M.addNode(new Node(1, 1));
		Node n3 = M.addNode(new Node(0, 1));

		/*
		 * method 1: create geometry directly from node
		 */
		// geometries
		Triangle t0 = new Triangle(n0, n1, n2);
		Triangle t1 = new Triangle(n0, n2, n3);
		M.addGeometry(t0);
		M.addGeometry(t1);

		/*
		 * method 2: create faces and create geometries based on faces
		 */
		/*
		 * TODO: need to create edges
		 */
		// IFace f0 = M.addFace(new FlatFace(n0, n1, n2));
		// IFace f1 = M.addFace(new FlatFace(n0, n2, n3));
		// Triangle t0 = new Triangle(f0);
		// Triangle t1 = new Triangle(f1);
		// M.addGeometry(t0);
		// M.addGeometry(t1);

		ElasticStructure struct = new ElasticStructure(M);
		RealTable realTable = new RealTable(1, 0.2);
		realTable.setThickness(1.0 / 20);

		// elements
		IElement e1 = struct.addElement(new CST(realTable, t0));
		struct.addElement(new CST(realTable, t1));

		// constraints
		Constraint c1 = new Constraint(false, false);
		n0.setConstraint(c1);
		n3.setConstraint(c1);

		EdgeLoad el = new EdgeLoad(n1, n2, new FunctionRToR() {

			@Override
			public double valueAt(double x) {
				return -2;
			}

			@Override
			public int getP() {
				return 0;
			}

			@Override
			public Interval getDomain() {
				return new Interval(0, 1);
			}

			@Override
			public double[] getBreakpoints() {
				return null;
			}

			@Override
			public double derivativeAt(double x) {
				return 0;
			}
		});

		e1.addEdgeLoad(el);

		return struct;
	}

	public ElasticStructure createModel2() {

		Mesh M = new Mesh();

		// nodes
		Node n0 = M.addNode(new Node(0, 0));
		Node n1 = M.addNode(new Node(100, 0));
		Node n2 = M.addNode(new Node(100, 100));
		Node n3 = M.addNode(new Node(0, 100));

		/*
		 * method 1: create geometry directly from node
		 */
		// geometries
		Triangle t0 = new Triangle(n0, n1, n2);
		Triangle t1 = new Triangle(n0, n2, n3);
		M.addGeometry(t0);
		M.addGeometry(t1);

		/*
		 * method 2: create faces and create geometries based on faces
		 */
		/*
		 * TODO: need to create edges
		 */
		// IFace f0 = M.addFace(new FlatFace(n0, n1, n2));
		// IFace f1 = M.addFace(new FlatFace(n0, n2, n3));
		// Triangle t0 = new Triangle(f0);
		// Triangle t1 = new Triangle(f1);
		// M.addGeometry(t0);
		// M.addGeometry(t1);

		ElasticStructure struct = new ElasticStructure(M);
		RealTable realTable = new RealTable(2e7, 0.3);
		realTable.setThickness(0.1);

		// elements
		struct.addElement(new CST(realTable, t0));
		struct.addElement(new CST(realTable, t1));

		// constraints
		Constraint c1 = new Constraint(false, false);
		n0.setConstraint(c1);
		n3.setConstraint(c1);

		n1.setForce(new Force(0, -1e5));

		return struct;
	}
}
